package com.video.common.utils;

import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.oss.ClientException;
import com.aliyun.tea.TeaException;

public class SendSmsUtil {
  /**
   * 使用AK&SK初始化账号Client
   * @param accessKeyId
   * @param accessKeySecret
   * @return Client
   * @throws Exception
   */
  public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
    com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
        // 您的 AccessKey ID
        .setAccessKeyId(accessKeyId)
        // 您的 AccessKey Secret
        .setAccessKeySecret(accessKeySecret);
    // 访问的域名
    config.endpoint = "dysmsapi.aliyuncs.com";
    return new com.aliyun.dysmsapi20170525.Client(config);
  }

  /**
   * 发送短信
   * @param phone 手机号
   * @param smsCode 模版代码
   * @param activeCode 短信6位码
   * @return SendSmsResponse
   * @throws ClientException e
   */
  public static SendSmsResponse sendSms(String phone, String smsCode, String activeCode) throws ClientException {
    com.aliyun.dysmsapi20170525.Client client = null;
    try {
      client = SendSmsUtil.createClient("LTAI5t9gneqxevUAKV8pb64E", "Nt9SvMbcEbzjedYK52boxPgXRhuXN3");
    } catch (Exception e) {
      e.printStackTrace();
    }
    com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
        .setSignName("抖视界")//签名
        .setTemplateCode(smsCode)//短信模版
        .setPhoneNumbers(phone)//发送手机号
        .setTemplateParam("{\"code\":\""+ activeCode + "\"}");//验证码

    com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
    try {
      // 复制代码运行请自行打印 API 的返回值
      return client.sendSmsWithOptions(sendSmsRequest, runtime);
    } catch (TeaException error) {
      // 如有需要，请打印 error
      com.aliyun.teautil.Common.assertAsString(error.message);
    } catch (Exception _error) {
      TeaException error = new TeaException(_error.getMessage(), _error);
      // 如有需要，请打印 error
      com.aliyun.teautil.Common.assertAsString(error.message);
    }
    return null;
  }

  public static void main(String[] args_) throws Exception {
    java.util.List<String> args = java.util.Arrays.asList(args_);
    com.aliyun.dysmsapi20170525.Client client = SendSmsUtil.createClient("LTAI5t9gneqxevUAKV8pb64E", "Nt9SvMbcEbzjedYK52boxPgXRhuXN3");
    com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
        .setSignName("抖视界")
        .setTemplateCode("SMS_253910178")
        .setPhoneNumbers("13217106697")
        .setTemplateParam("{\"code\":\"123456\"}");

    com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
    try {
      // 复制代码运行请自行打印 API 的返回值
      client.sendSmsWithOptions(sendSmsRequest, runtime);
    } catch (TeaException error) {
      // 如有需要，请打印 error
      com.aliyun.teautil.Common.assertAsString(error.message);
    } catch (Exception _error) {
      TeaException error = new TeaException(_error.getMessage(), _error);
      // 如有需要，请打印 error
      com.aliyun.teautil.Common.assertAsString(error.message);
    }
  }

}
